package com.zoe.wan.android.example

import android.graphics.BitmapFactory
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.zoe.wan.android.example.databinding.ActivityTabBinding
import com.zoe.wan.base.BaseActivity
import com.zoe.wan.android.example.BR
import com.zoe.wan.android.example.fragment.home.FragHome
import com.zoe.wan.android.example.fragment.hot_key.FragHotKey
import com.zoe.wan.android.example.fragment.knowledge.FragKnowledge
import com.zoe.wan.android.example.fragment.personal.FragPersonal
import com.zoe.wan.base.adapter.Pager2Adapter
import com.zoe.wan.base.tab.NavigationBottomBar

class TabActivity : BaseActivity<ActivityTabBinding, TabViewModel>() {
    override fun getLayoutId() = R.layout.activity_tab

    override fun getViewModelId() = BR.tabVm
    override fun initViewData() {
        initPages()
        val icons = arrayOf(
            BitmapFactory.decodeResource(resources, R.drawable.icon_home_selected),
            BitmapFactory.decodeResource(resources, R.drawable.icon_hot_key_selected),
            BitmapFactory.decodeResource(resources, R.drawable.icon_knowledge_selected),
            BitmapFactory.decodeResource(resources, R.drawable.icon_personal_selected),
        )

        val iconGreys = arrayOf(
            BitmapFactory.decodeResource(resources, R.drawable.icon_home_grey),
            BitmapFactory.decodeResource(resources, R.drawable.icon_hot_key_grey),
            BitmapFactory.decodeResource(resources, R.drawable.icon_knowledge_grey),
            BitmapFactory.decodeResource(resources, R.drawable.icon_personal_grey),
        )

        val tabText = arrayOf(
            "首页",
            "热点",
            "体系",
            "个人",
        )

        binding?.tabBottomBar?.let {
            it.setSelectedIcons(icons.toList())
            it.setUnselectIcons(iconGreys.toList())
            it.setTabText(tabText.toList())
            it.setupViewpager(binding?.tabViewPager2)
            it.start()

            binding?.tabBottomBar?.registerTabClickListener( object :NavigationBottomBar.OnBottomTabClickListener {
                override fun tabClick(position: Int) {

                }
            })
        }
    }


    fun initPages() {
        val tabPages = mutableListOf<Fragment>(
            FragHome(),
            FragHotKey(),
            FragKnowledge(),
            FragPersonal(),
        )

        val adapter = Pager2Adapter(this)
        adapter.setData(tabPages)

        binding?.tabViewPager2?.offscreenPageLimit = ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT
        binding?.tabViewPager2?.adapter = adapter
    }
}